package com.powernobug.service.impl;

import com.github.pagehelper.PageHelper;
import com.powernobug.bean.model.MarketAdmin;
import com.powernobug.bean.model.MarketAdminExample;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.param.AdminListParam;
import com.powernobug.mapper.MarketAdminMapper;
import com.powernobug.service.AdminAdminService;
import com.powernobug.util.MyBatisUtil;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @Author: 肖云戈
 * @CreateTime: 2024-08-23
 * @Description:
 */
public class AdminAdminServiceImpl implements AdminAdminService {
    private static SqlSession tempSession;

    @Override
    public PageVo getAdminByPage(AdminListParam params) {
        MarketAdminExample example = new MarketAdminExample();
        if (params.getPage() == null || params.getLimit() == null) {
            throw new PersistenceException();
        }
        if (!(params.getSort() == null) && !(params.getOrder() == null)) {
            example.setOrderByClause(params.getSort() + " " + params.getOrder());
        }
        MarketAdminExample.Criteria criteria = example.createCriteria();
        if (params.getUserName() != null) {
            criteria.andUsernameEqualTo(params.getUserName());
        }
        criteria.andDeletedEqualTo(false);

        tempSession = MyBatisUtil.getSqlSession();
        MarketAdminMapper adminMapper = tempSession.getMapper(MarketAdminMapper.class);
        PageHelper.startPage(params.getPage(), params.getLimit());
        List<MarketAdmin> targetAdmins = adminMapper.selectByExample(example);
        MyBatisUtil.releaseSqlSession(tempSession);
        return PageVo.pag(targetAdmins);
    }

    @Override
    public MarketAdmin create(MarketAdmin marketAdmin) {
        marketAdmin.setAddTime(new Date());
        marketAdmin.setUpdateTime(new Date());
        marketAdmin.setDeleted(false);

        tempSession = MyBatisUtil.getSqlSession();
        MarketAdminMapper mapper = tempSession.getMapper(MarketAdminMapper.class);
        mapper.insert(marketAdmin);

        MyBatisUtil.releaseSqlSession(tempSession);
        return marketAdmin;
    }

    @Override
    public boolean update(MarketAdmin marketAdmin, Boolean deleteMode) {
        tempSession = MyBatisUtil.getSqlSession();
        MarketAdminMapper mapper = tempSession.getMapper(MarketAdminMapper.class);
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketAdmin.getId());
        marketAdmin.setDeleted(false);
        if (deleteMode) {
            marketAdmin.setDeleted(true);
            criteria.andUsernameEqualTo(marketAdmin.getUsername());
            criteria.andPasswordEqualTo(marketAdmin.getPassword());
        }
        criteria.andDeletedEqualTo(false);

        MarketAdmin verify = mapper.selectByExample(example).get(0);

        if (verify == null) {
            return false;
        } else if (!deleteMode && (marketAdmin.getDeleted() != null && marketAdmin.getDeleted())) {
            return false;
        }

        marketAdmin.setUpdateTime(new Date());
        mapper.updateByPrimaryKey(marketAdmin);
        MyBatisUtil.releaseSqlSession(tempSession);
        return true;
    }
}
